package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bean.*;
import DAO.*;

import service.*;

/**
 * 主人管理Servlet，处理商品删除、增加、修改、新增等操作
 * @author Administrator
 *
 */
public class DesServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public DesServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		//获取前台参数，根据参数选择对应的servlet方法进行业务处理
		String op = request.getParameter("op");
		
		if("update".equals(op)){// 更新商品
			updateService(request,response);
		}else if("del".equals(op)){//删除商品
			deleteService(request,response);
		}else if("insert".equals(op)){//添加商品
			insertService(request,response);
		}else if("select".equals(op)){//查询商品
			selectService(request,response);
		}
		
		
	}
	/**
	 * 	宠物信息
	 * @param response
	 */
	private void selectService(HttpServletRequest request,
			HttpServletResponse response) {
		
		IMasterDao dao = DaoFactory.getMasterDao();
		  Map<String,Object> map = null;

		  try{
		  if(dao.queryMaster(map)!=null){
			  request.getRequestDispatcher("阅览病例.jsp").forward(request, response);
		  }else{
			  response.sendRedirect("main1.jsp");
		  }
		  }
		  catch(Exception e){
			  e.printStackTrace();
		  }
	}
	/**
	 * 增加宠物
	 * @param request
	 * @param response
	 */
	private void insertService(HttpServletRequest request,
			HttpServletResponse response) {
		
		//获取前台传递的三个参数，其中id在数据中使用自增字段，无需手动输入
		int pet_id=Integer.parseInt(request.getParameter("pet_id"));
		String doctor=request.getParameter("doctor");
		String sickness=request.getParameter("sickness");
		String data=request.getParameter("data");
        Descriptionbean p=new Descriptionbean(pet_id, doctor, data, sickness);
		IDesDao dao = DaoFactory.getDesDao();
		try{
		if(dao.insert(p)){
			request.getRequestDispatcher("阅览病例.jsp").forward(request, response);
		}else{
			response.sendRedirect("增加新病例.jsp");
		}}
		catch(Exception e){
			e.printStackTrace();
		}
		
	}
	/**
	 * 删除病例
	 * @param request
	 * @param response
	 */
	private void deleteService(HttpServletRequest request,
			HttpServletResponse response) {
		
		//接收前台传递的待删除商品ID，根据id删除对应商品
		//delete from product where id ="id";
		int pet_id =Integer.parseInt(request.getParameter("pet_id"));
		Descriptionbean p = new Descriptionbean();
		p.setPet_id(pet_id);
		IDesDao dao = DaoFactory.getDesDao();
		try{
		if(dao.delete(p)){
			request.getRequestDispatcher("阅览病例.jsp").forward(request, response);
			
		}else{
			response.sendRedirect("阅览病例.jsp");
		}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	/**
	 * 更新
	 * @param request
	 * @param response
	 */
	private void updateService(HttpServletRequest request,
			HttpServletResponse response) {
		
	}

	public void init() throws ServletException {
	}

}
